/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HandleDataTableCampaignHourly {
public static final String KEYSPACE                         =   "AdsData";
public static final String ColumFamly                       =   "BannerViewCountCf";
public static final String ColumFamlyBannerClickCountCf     =   "BannerClickCountCf";
public static final String ColumFamlyBannerViewCountCf      =   "BannerViewCountCf";

public static final String ColumFamlyCampaignClickCountCf       =   "CampaignCountCf";
public static final String ColumFamlyCampaignViewCountCf        =   "CampaignViewCountCf";

public void handleData() throws ParseException
{
   //handle data by v3_cam_date

    long first_time = System.currentTimeMillis();
    CassandraAction cassAct = new CassandraAction(ACCOUNT.CLUSTERNAME ,ACCOUNT.URL, ACCOUNT.PORT);
    cassAct.initReadWirte(KEYSPACE);
    MysqlDataManager mysqldatamanager = new MysqlDataManager();
    CqlDataManager cqldatamanager = new CqlDataManager();
    List listbanners = cqldatamanager.getAllCampainID();

    String currentDate = TimeUtil.getCurrentTimeInString();//2012-07-24
    String[] arrStr = new String[3];
    arrStr = currentDate.split("-");
    String changeDay = arrStr[2]+"/"+arrStr[1]+"/"+arrStr[0];
    StringBuilder sbff = new StringBuilder();

    Iterator it=listbanners.iterator();
    while(it.hasNext())
    {
        String key = (String)it.next();

        if(key.length() > 1)
        {
            String str = getClickCampaignHour(mysqldatamanager, cassAct, changeDay, changeDay, key, ColumFamlyCampaignClickCountCf, ColumFamlyCampaignViewCountCf);
            sbff.append(str);
            if(sbff.toString().length() > 10000)
            {
                //xu li cho du lieu cuoi
                String relStr = sbff.toString();
                String valInsert = relStr.substring(0, (relStr.length()-1));
                mysqldatamanager.insertDataCampainHour(valInsert);
                sbff.setLength(0);
            }
        }else {
            Debug.println(key);
            Debug.println("fail");
            long last_time = System.currentTimeMillis();
            System.out.println("Thoi gian chay  :" +(last_time-first_time));
            continue;
        }
        if(sbff.toString().length() > 1)
        {
            //xu li cho du lieu cuoi
            String relStr = sbff.toString();
            String valInsert = relStr.substring(0, (relStr.length()-1));
            mysqldatamanager.insertDataCampainHour(valInsert);
            sbff.setLength(0);
        }
    }
    long last_time = System.currentTimeMillis();
    System.out.println("Thoi gian chay  :" +(last_time-first_time));
    Debug.println("success HandleDataTableCampaignHourly");
}

public static String getClickCampaignHour(MysqlDataManager mysqldatamanager,CassandraAction cassAct,String startDay,String finishDay,String key,String columnFamilyClick,String columnFamilyView) throws ParseException{

        StringBuilder sb = new StringBuilder();
        String[] arrfirst = new String[3];
        String[] arrlast = new String[3];
        String dayStart = startDay.split("/")[0];
        String dayLast = finishDay.split("/")[0];
        String prefix = "/" +startDay.split("/")[1]+"/"+startDay.split("/")[2];

        String prefixDB = startDay.split("/")[2] + "-" + startDay.split("/")[1] + "-";

        for(int i = Integer.valueOf(dayStart) ; i <= Integer.valueOf(dayLast) ;i++)
        {
            for(int hour =0 ;hour <=23 ;hour ++)
            {
                if(hour < 10)
                {
                    String date = "0"+ hour +":00:00 " + String.valueOf(i) + prefix ;
                    long start_time = TimeUtil.longToDateWithHour(date);
                    //them 1h
                    long finish_time = start_time + 60*60*1000;
                    long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);
                    long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);
                    String str = "(" + key + "," + hour + "," + countClick + "," + countView + ",'" + (prefixDB +i) + "')";
                    sb.append(str).append(",");
                    //mysqldatamanager.insertDataCampainHour(key,hour, String.valueOf(countClick), String.valueOf(countView), (prefixDB +i));
                } else {
                    String date = hour +":00:00 " + String.valueOf(i) + prefix ;
                    long start_time = TimeUtil.longToDateWithHour(date);
                    //them 1h
                    long finish_time = start_time + 60*60*1000;
                    long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);
                    long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);
                    String str = "(" + key + "," + hour + "," + countClick + "," + countView + ",'" + (prefixDB +i) + "')";
                    sb.append(str).append(",");
                    //mysqldatamanager.insertDataCampainHour(key,hour, String.valueOf(countClick), String.valueOf(countView), (prefixDB +i));
                }
            }
        }
        return sb.toString();
    }

}
